package com.bsoft.gol.hcs.utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

/**
 * xml字符串转换成json字符串
 * Created by yangyi on 2016/8/22.
 */
public class XmlToJson {

	/**
	 * xml字符串转换成json字符串
	 */
	public static String xml2jsonString(String xml) throws JSONException {
		JSONObject xmlJSONObj = XML.toJSONObject(xml);
		return xmlJSONObj.toString();
	}

	/**
	 * Json多层数据结构转换成xml
	 * @param jsonStr
	 * @return
	 */
	public static String Json2XmlString(String jsonStr){
		System.out.println("json转换成xmlString:");
		com.alibaba.fastjson.JSONObject json = com.alibaba.fastjson.JSONObject.parseObject(jsonStr);
		StringBuffer sb = new StringBuffer();
		for(Object key : json.keySet()){
			sb.append("<").append(key).append(">");
			Object value = json.get(key);
			sb.append(iteraorJson(value));
			sb.append("</").append(key).append(">");
		}
		return sb.toString();
	}

	/**
	 * 迭代判断value是否还包含jSONObject
	 * @param value
	 * @return
	 */
	public static String iteraorJson(Object value){
		StringBuffer sb = new StringBuffer("");
		if((value.toString().contains(":"))){
			com.alibaba.fastjson.JSONObject json = ((com.alibaba.fastjson.JSONObject)value);
			for(Object key : json.keySet()){
				sb.append("<").append(key).append(">");
				Object value2 = json.get(key);
				sb.append(iteraorJson(value2));
				sb.append("</").append(key).append(">");
			}
		}else{
			sb.append(value);
		}
		return sb.toString();
	}
	/*public static  String xml2JSON(String xml) {
	    JSONObject obj = new JSONObject();
	    try {
	        InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));
	        SAXBuilder sb = new SAXBuilder();
	        Document doc = sb.build(is);
	        Element root = doc.getRootElement();
	        obj.put(root.getName(), iterateElement(root));
	        return obj.toString();
	    } catch (Exception e) {
	        e.printStackTrace();
	        return null;
	    }
	}
	private static Map iterateElement(Element element) {
	    List jiedian = element.getChildren();
	    Element et = null;
	    Map obj = new HashMap();
	    List list = null;
	    for (int i = 0; i < jiedian.size(); i++) {
	        list = new LinkedList();
	        et = (Element) jiedian.get(i);
	        if (et.getTextTrim().equals("")) {
	            if (et.getChildren().size() == 0)
	                continue;
	            if (obj.containsKey(et.getName())) {
	                list = (List) obj.get(et.getName());
	            }
	            list.add(iterateElement(et));
	            obj.put(et.getName(), list);
	        } else {
	            if (obj.containsKey(et.getName())) {
	                list = (List) obj.get(et.getName());
	            }
	            list.add(et.getTextTrim());
	            obj.put(et.getName(), list);
	        }
	    }
	    return obj;
	}*/

	public static String getXML2(String json)
	{
		StringBuffer sBuffer = new StringBuffer("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempurl.org\">");
		sBuffer.append(" <soapenv:Header/>");
		sBuffer.append(" <soapenv:Body>");
		sBuffer.append(" <tem:nf_hai_funmain>");
		sBuffer.append("<tem:as_instr>");
		sBuffer.append(json);
		sBuffer.append("</tem:as_instr>");
		sBuffer.append("</tem:nf_hai_funmain>");
		sBuffer.append("</soapenv:Body>");
		sBuffer.append("</soapenv:Envelope>");

		return sBuffer.toString();
	}
}
